package com.ali.service.impl;

import com.ali.common.Response;
import com.ali.common.ReturnEnum;
import com.ali.service.SentinelService;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class SentinelServiceImpl implements SentinelService {
    /**
     * SentinelResource：Sentinel资源
     */
    @SentinelResource(value = "hello")
    @Override
    public Response<String> hello(Map<String, Object> param) {
        return Response.success("hello");
    }

    /**
     * SentinelResource：Sentinel资源
     */
    @SentinelResource(value = "world", blockHandler = "myBlockHandler", fallback = "myFallback")
    @Override
    public Response<String> world(Map<String, Object> param) {
        return Response.success("world");
    }

    /**
     * Sentinel自定义服务限流方法
     */
    public Response<String> myBlockHandler(Map<String, Object> param, BlockException blockException) {
        return Response.fail(ReturnEnum.RC999.getCode(), "服务限流，请稍后再试：" + param);
    }

    /**
     * Sentinel自定义服务降级方法
     */
    public Response<String> myFallback(Map<String, Object> param, Throwable t) {
        return Response.fail(ReturnEnum.RC999.getCode(), "服务降级，请稍后再试：" + param);
    }
}